﻿using System.Collections;

namespace DelegateT1
{
    //? 一个代理类型
    public delegate void StringAssignmentEventHandler();

    public class InvokeList
    {
        //? 代理集合，内含一组具体方法
        private IList<StringAssignmentEventHandler> handlers;
        private string[] message = new string[3];

        public InvokeList()
        {
            handlers = new List<StringAssignmentEventHandler>();
            handlers.Add(AppendHello);
            handlers.Add(AppendComma);
            handlers.Add(AppendWorld);
        }

        //? 调用这一组具体方法，客户端只用调用这一个方法就可以调用所有具体方法。
        public void Invoke()
        {
            foreach (StringAssignmentEventHandler handler in handlers)
            {
                handler();
            }
        }

        public string this[int index]
        {
            get { return message[index]; }
        }

        public void AppendHello()
        {
            message[0] = "Hello";
        }

        public void AppendComma()
        {
            message[1] = ",";
        }

        public void AppendWorld()
        {
            message[2] = "World!";
        }
    }
}